<?php

namespace common\models\navigation;

use Yii;

/**
 * This is the model class for table "{{%navigation}}".
 *
 * @property int $id
 * @property string $title 导航名称
 * @property string|null $position 位置
 * @property string|null $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Navigation extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%navigation}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['title'], 'required'],
            [['columns', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['title'], 'string', 'max' => 100],
            [['position'], 'string', 'max' => 50],
            [['mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => '导航名称',
            'position' => '位置',
            'mark' => '备注',
            'columns' => '每行列数',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getItems() {
        return $this->hasMany(NavigationItem::class, [
            'navigation_id' => 'id'
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getMenus() {
        return $this->getItems()->active(NavigationItem::STATUS_ACTIVE)->orderBy([
            'listorder' => SORT_ASC
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function findByName($name) {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere([
            'position' => $name
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'title',
            'columns',
            'menus',
        ];
    }

}
